package com.dmp.onlinestore.apps;

import java.util.List;
//import com.dmp.onlinestore.model.service.*;
import com.dmp.onlinestore.model.dto.*;
import com.dmp.onlinestore.model.dao.*;
import com.dmp.onlinestore.model.dao.base.DaoException;
import com.dmp.onlinestore.model.dao.base.DaoFactory;
import com.dmp.onlinestore.model.dao.base.DaoTransaction;
import com.dmp.onlinestore.model.dao.testmemdb.base.InMemDatabase;

public class TestDbAccess {

	/**
	 * @param args
	 */
	
	//private boolean throwException = true;
	
	public static void main(String[] args) {
		testProductList();
		testProductById();
		//testPersonList();   not yet converted from other project
		//testAddPerson();    not yet converted from other project
		
		dumpInMemDb();
		

//		// Playing with exception handling...
//		TestDbAccess test = new TestDbAccess();
//		try {
//			System.out.println("Calling testException...");
//			test.testException();
//		}
//		catch (DaoException e) {
//			System.out.println("Caught exception: " + e.getErrorString());
//		}
//		System.out.println("End of main");
		
	}

	public TestDbAccess() {
		
	}
	
//	private void testException() throws DaoException {
//		try {
//			System.out.println("Inside testException's try clause");
//			if (this.throwException) {
//				System.out.println("About to throw #1");
//				throw new DaoException("Throw #1");
//			}
//			System.out.println("Made it to after throw #1");
//		}
//		catch (DaoException e) {
//			System.out.println("Caught exception: " + e.getErrorString());
//			throw new DaoException("Throw #2");
//		}
//		finally {
//			System.out.println("Inside testException's finally");
//		}
//		System.out.println("At end of testException");
//	}
	
	
	private static void testProductList() {
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			List<ProductBean> productList = dao.getProducts(ProductBeanFilterSpec.getDefaultFilter());
			transaction.finish();
			for (ProductBean product : productList) {
				System.out.println(product.toString());
			}
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
	}
	
	private static void testProductById() {
		try {
			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
			ProductDao dao = DaoFactory.instance().getProductDao(transaction);
			int productId = 10001;
			ProductBean product = dao.getProduct(productId);
			transaction.finish();
			if (product == null) {
				System.out.println("No product found for id \"" + productId + "\"");
			}
			else {
				System.out.println(product.toString());
			}
		}
		catch (DaoException e) {
			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
		}
	}
	
	private static void dumpInMemDb() {
		InMemDatabase inMemDb = InMemDatabase.instance();
		inMemDb.dump();
	}
	
	
	
//	private static void testPersonList() {
//		try {
//			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
//			PersonDao dao = DaoFactory.instance().getPersonDao(transaction);
//			List<PersonBean> personList = dao.getPersons();
//			transaction.finish();
//			for (PersonBean person : personList) {
//				System.out.println(person.toString());
//			}
//		}
//		catch (DaoException e) {
//			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
//			e.printStackTrace();
//		}
//	}
//	
//	private static void testPersonById() {
//		try {
//			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
//			PersonDao dao = DaoFactory.instance().getPersonDao(transaction);
//			PersonBean person = dao.getPersonById(1);
//			transaction.finish();
//			System.out.println(person.toString());
//		}
//		catch (DaoException e) {
//			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
//			e.printStackTrace();
//		}
//	}
//
//	private static void testAddPerson() {
//		try {
//			DaoTransaction transaction = DaoFactory.instance().getDaoTransaction();
//			PersonDao dao = DaoFactory.instance().getPersonDao(transaction);
//			PersonBean person = new PersonBean("Mark", "John", "Holden");
//			dao.addPerson(person);
//			transaction.finish();
//			System.out.println(person.toString());
//		}
//		catch (DaoException e) {
//			System.out.println("Dao exception: \"" + e.getErrorString() + "\"");
//			e.printStackTrace();
//		}
//	}

}
